
"""
    pycoopiler - a compiler developed for PCS2056 course (Compilers) at Poli-USP
    Copyright (C) 2008 Andre Felipe Santos    
    Copyright (C) 2008 Rodrigo Rodrigues da Silva

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
"""

from pycoopiler.token import *
from pycoopiler.enum import enum
from pycoopiler.submaquina import submaquina
from pycoopiler.maq_chfunc import maq_chfunc
from pycoopiler.maq_explog import maq_explog
from pycoopiler.maq_exparith import maq_exparith

class maq_cmds(submaquina):
    
    states = enum(" _0 \
                    _2 \
                    _3 \
                    _5 \
                    _6 \
                    _7 \
                    _8 \
                    _10 \
                    _12 \
                    _13 \
                    _34 \
                    _37 \
                    _38 \
                    _39 \
                    _41 \
                    _40")
    
    def __init__(self, _tkzr):
        self.tkzr = _tkzr     
        self.state = self.states._0
        self.name = "CMDS"
        self.log_chamada(self.name)
        self.current = self.tkzr.current
        
    def analyze(self):
        
        while not (self.state == self.states._0 and ( \
                    self.current.type != self.current.types.ID and\
                    self.current.type != self.current.types.IF and\
                    self.current.type != self.current.types.WHILE)):
            
            self.log_state()  
              
            if self.state == self.states._0:
                if self.current.type == self.current.types.ID:
                    self.state = self.states._3
                elif self.current.type == self.current.types.IF:
                    self.state = self.states._34
                elif self.current.type == self.current.types.WHILE:
                    self.state = self.states._34
                else: raise Exception ("Erro Sintatico! Esperando IF, ID ou WHILE")
                self.consume()
            
            elif self.state == self.states._2:
                if self.current.type == self.current.types.SEMICOLON:
                    self.state = self.states._0
                else: raise Exception ("Erro Sintatico! Esperando SEMICOLON")
                self.consume()
            
            elif self.state == self.states._3:
                if self.current.type == self.current.types.OPEN_SB:
                    self.state = self.states._5
                    self.consume()
                elif self.current.type == self.current.types.ATTR:
                    self.state = self.states._8
                    self.consume()
                else: 
                    m = maq_chfunc(self.tkzr)
                    try: m.analyze()
                    except: raise
                    self.current = self.tkzr.current
                    self.state = self.states._2

            
            elif self.state == self.states._5:
                if self.current.type == self.current.types.CONST:
                    self.state = self.states._6
                else: raise Exception ("Erro Sintatico! Esperando CONST")
                self.consume()               
            
            elif self.state == self.states._6:
                if self.current.type == self.current.types.CLOSE_SB:
                    self.state = self.states._7
                else: raise Exception ("Erro Sintatico! Esperando CLOSE_SB")
                self.consume()
            
            elif self.state == self.states._7:
                if self.current.type == self.current.types.ATTR:
                    self.state = self.states._8
                else: raise Exception ("Erro Sintatico! Esperando ATTR")
                self.consume()
            
            elif self.state == self.states._8:
                if self.current.type == self.current.types.ID:
                    self.state = self.states._10
                    self.consume()
                else: 
                    m = maq_exparith(self.tkzr)
                    try: m.analyze()
                    except: raise
                    self.current = self.tkzr.current
                    self.state = self.states._2
              
            elif self.state == self.states._10:
                if self.current.type == self.current.types.OPEN_SB:
                    self.state = self.states._12
                elif self.current.type == self.current.types.SEMICOLON:
                    self.state = self.states._0
                else: raise Exception ("Erro Sintatico! Esperando OPEN_SB ou SEMICOLON")
                self.consume()
            
            elif self.state == self.states._12:
                if self.current.type == self.current.types.CONST:
                    self.state = self.states._13
                else: raise Exception ("Erro Sintatico! Esperando CONST")
                self.consume()
            
            elif self.state == self.states._13:
                if self.current.type == self.current.types.CLOSE_SB:
                    self.state = self.states._2
                else: raise Exception ("Erro Sintatico! Esperando CLOSE_SB")
                self.consume()
            
            elif self.state == self.states._34:
                if self.current.type == self.current.types.OPEN_RB:
                    self.state = self.states._37
                else: raise Exception ("Erro Sintatico! Esperando OPEN_RB")
                self.consume()               
            
            elif self.state == self.states._37:
                m = maq_explog(self.tkzr)
                try: m.analyze()
                except: raise
                self.current = self.tkzr.current
                self.state = self.states._38
            
            elif self.state == self.states._38:
                if self.current.type == self.current.types.CLOSE_RB:
                    self.state = self.states._39
                else: raise Exception ("Erro Sintatico! Esperando CLOSE_RB")
                self.consume()
            
            elif self.state == self.states._39:
                if self.current.type == self.current.types.OPEN_CB:
                    self.state = self.states._40
                else: raise Exception ("Erro Sintatico! Esperando OPEN_CB")
                self.consume()
            
            elif self.state == self.states._40:
                m = maq_cmds(self.tkzr)
                try: m.analyze()
                except: raise
                self.current = self.tkzr.current
                self.state = self.states._41
            
            elif self.state == self.states._41:
                if self.current.type == self.current.types.CLOSE_CB:
                    self.state = self.states._0
                else: raise Exception ("Erro Sintatico! Esperando CLOSE_CB")
                self.consume()
            
            else: raise Exception("ERRO: Estado invalido: " + self.state)
        
        self.log_retorno(self.name)